(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.lookup.repaymentListController", listController);
	listController.$inject = [ "$scope", "$uibModalInstance", "dialogsModule", "modalService", "gridService", "layoutConfig", "validatorConfig", "authService" ];
	function listController($scope, $uibModalInstance, dialogsModule, modalService, gridService, layoutConfig, validatorConfig, authService) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.searchCriteria = ctrl.searchCriteria || {};

		ctrl.init = function() {
			ctrl.repaymentGridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.repaymentGridOptions.data = "vm.searchCriteria.list";
			ctrl.repaymentGridOptions.multiSelect = false;
			ctrl.repayTypeOptions = [ {
				"value" : "P",
				"label" : "label.payment.repayment"
			}, {
				"value" : "M",
				"label" : "label.payment.remittance"
			}, {
				"value" : "W",
				"label" : "label.payment.writeoff"
			}, {
				"value" : "R",
				"label" : "label.payment.rollover"
			}, {
				"value" : "B",
				"label" : "label.payment.billing"
			} ];
			ctrl.repayStatusOptions = [{
				"value": "L",
				"label": "label.payment.paystatus.l"
			}, {
				"value": "X",
				"label": "label.payment.paystatus.x"
			}, {
				"value": "R",
				"label": "label.payment.paystatus.r"
			}, {
				"value": "W",
				"label": "label.payment.paystatus.w"
			}];
			ctrl.searchCriteria = ctrl.repaymentGridOptions.defaultSearchCriteria;
			//if (ctrl.dialogsModule.params.loanId) {
				ctrl.searchCriteria.loanId = 0;
			//} else {
			//	return;
			//}
			if (ctrl.dialogsModule.params.repayStatus) {
				ctrl.searchCriteria.repayStatus = ctrl.dialogsModule.params.repayStatus;
			}
			ctrl.search();
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.repaymentGridOptions);
			}, angular.noop);
		};

		ctrl.viewRepayment = function(currRow) {
			modalService.openCustomDialogs({
				"title" : "popup.title.repaymentview",
				"dialogsModule" : "cls.loan.repaymentView",
				"params" : {"repayId" : currRow.entity.repayId},
				"btnCallback" : {
					"ok" : function(obj) {
					}
				}
			});
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : "";
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : "";
			ctrl.search();
		}

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](ctrl.selectedRows) !== false) {
				$uibModalInstance.close();
			}
		};


	}
})(angular);